//
//  DYTabBarController.m
//  BaiSi
//
//  Created by mac on 16/10/14.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "DYTabBarController.h"
#import "DYNavigationController.h"


#import "DYEssenceViewController.h"
#import "DYNewViewController.h"
#import "DYPublishViewController.h"
#import "DYFriendTrendsViewController.h"
#import "DYMeTableViewController.h"





@interface DYTabBarController ()<UITabBarControllerDelegate>
@property(nonatomic,weak) UIViewController *lastVc;
@end

@implementation DYTabBarController

+ (void)load {
    //设置颜色
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSForegroundColorAttributeName] = [UIColor blackColor];
    [item setTitleTextAttributes:attr forState:UIControlStateSelected];
    
    //设置字体大小
    NSMutableDictionary *attr1 = [NSMutableDictionary dictionary];
    attr1[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    [item setTitleTextAttributes:attr1 forState:UIControlStateNormal];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //加载所有控制器
    [self setUpAllChiledControllers];
    
    //设置tabBar栏内容
    [self setUpTabBarItemContent];
    
    //设置中间按钮
    [self setUpPlusBtn];
    
    //代理
    self.delegate = self;
    //上一个选中的控制器
    _lastVc = self.childViewControllers.firstObject;
}

#pragma mark - UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
   //重复点击tabbar栏按钮,重新刷新
    if (viewController == _lastVc) {
        [[NSNotificationCenter defaultCenter] postNotificationName:DYReloadThemeView object:nil];
    }
    //重新赋值
    _lastVc = viewController;
}

#pragma mark - 添加中间按钮

- (void)setUpPlusBtn {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateHighlighted];
    [button sizeToFit];
    [self.tabBar addSubview:button];
    button.center = CGPointMake(DYScreenW * 0.5, self.tabBar.bounds.size.height * 0.5);
    
  
    
}

#pragma mark - 设置tabBar栏内容
- (void)setUpTabBarItemContent {
    //精华
    UIViewController *Vc0 = self.childViewControllers[0];
    Vc0.tabBarItem.title = @"精华";
    Vc0.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
    Vc0.tabBarItem.selectedImage =  [UIImage imageWithOriImage:@"tabBar_essence_click_icon"];
    
    //新帖
    UIViewController *Vc1 = self.childViewControllers[1];
    Vc1.tabBarItem.title = @"新帖";
    Vc1.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
    Vc1.tabBarItem.selectedImage =  [UIImage imageWithOriImage:@"tabBar_new_click_icon"];
   
    
    //发布
    UIViewController *Vc2 = self.childViewControllers[2];
    Vc2.tabBarItem.enabled = NO;
    
    //关注
    UIViewController *Vc3 = self.childViewControllers[3];
    Vc3.tabBarItem.title = @"关注";
    Vc3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
    Vc3.tabBarItem.selectedImage =  [UIImage imageWithOriImage:@"tabBar_friendTrends_click_icon"];
    
    //我
    UIViewController *Vc4 = self.childViewControllers[4];
    Vc4.tabBarItem.title = @"我";
    Vc4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
    Vc4.tabBarItem.selectedImage =  [UIImage imageWithOriImage:@"tabBar_me_click_icon"];
}


#pragma mark - 加载所有控制器
- (void)setUpAllChiledControllers {
    
    //精华
    DYEssenceViewController *essenceVc = [[DYEssenceViewController alloc] init];

    DYNavigationController *nav0 = [[DYNavigationController alloc] initWithRootViewController:essenceVc];
    [self addChildViewController:nav0];
    
     //新帖
    DYNewViewController *newVc = [[DYNewViewController alloc] init];

    DYNavigationController *nav1 = [[DYNavigationController alloc] initWithRootViewController:newVc];
    [self addChildViewController:nav1];
    
    //发布
    DYPublishViewController *publishVc = [[DYPublishViewController alloc] init];

    DYNavigationController *nav2 = [[DYNavigationController alloc] initWithRootViewController:publishVc];
    [self addChildViewController:nav2];
    
    //关注
    DYFriendTrendsViewController *friendVc = [[DYFriendTrendsViewController alloc] init];

    DYNavigationController *nav3 = [[DYNavigationController alloc] initWithRootViewController:friendVc];
    [self addChildViewController:nav3];
    
    //我
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"DYMeTableViewController" bundle:nil];
    DYMeTableViewController *meVc = [storyboard instantiateInitialViewController];

    DYNavigationController *nav4 = [[DYNavigationController alloc] initWithRootViewController:meVc];
    [self addChildViewController:nav4];

}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
